mqttrs 0.2.0

mqttrs is encoding & decoding library for mqtt protocol, it can work with both sync as well as async apps
Documentation

Rust Mqtt Encoding & Decoding Crates.io Docs.rs

Mqttrs is a Rust crate (library) to write MQTT protocol clients and servers.

It is a codec-only library with very few dependencies and a straightworward and composable API, usable with rust's standard library or with async frameworks like tokio.

Mqttrs currently requires Rust >= 1.32 and supports MQTT 3.1.1. Support for MQTT 5 is planned for a future version.

Usage

Add mqttrs = "0.2" to your Cargo.toml.

use mqttrs::*;
use bytes::BytesMut;

// Allocate write buffer.
let mut buf = BytesMut::with_capacity(1024);

// Encode an MQTT Connect packet.
let pkt = Packet::Connect(Connect { protocol: Protocol::MQTT311,
                                    keep_alive: 30,
                                    client_id: "doc_client".into(),
                                    clean_session: true,
                                    last_will: None,
                                    username: None,
                                    password: None });
assert!(encode(&pkt, &mut buf).is_ok());
assert_eq!(&buf[14..], "doc_client".as_bytes());
let mut encoded = buf.clone();

// Decode one packet. The buffer will advance to the next packet.
assert_eq!(Ok(Some(pkt)), decode(&mut buf));

// Example decode failures.
let mut incomplete = encoded.split_to(10);
assert_eq!(Ok(None), decode(&mut incomplete));
let mut garbage = BytesMut::from(vec![0u8,0,0,0]);
assert_eq!(Err(Error::InvalidHeader), decode(&mut garbage));